package com.eee.machine.adminManager;

import com.eee.machine.beans.TUser;
import com.eee.machine.common.OperateResponse;
import com.eee.machine.configuration.StoreConfig;
import com.eee.machine.service.ITokenService;
import com.eee.machine.service.IUserService;
import com.eee.machine.utils.cache.SimpleCache;
import com.google.common.base.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping(value = "/admin")
public class AdminManager {

    @Autowired
    private IUserService userService;
    @Autowired
    private HttpSession session;
    @Autowired
    private StoreConfig storeConfig;
    @Autowired
    private ITokenService tokenService;

    @RequestMapping("/login")
    String loginManager(){
        return "login";
    }

    @RequestMapping("/adminLogin")
    @ResponseBody
    OperateResponse adminLogin(TUser user){
        return userService.checkAdminLogin(user.getPhone(), user.getPassword());
    }

    @RequestMapping("/loginOut")
    String loginOut(){
        SimpleCache.getInstance().removeCache("userInfo");
        session.invalidate();
        return "login";
    }

    @RequestMapping("/setCode")
    @ResponseBody
    OperateResponse setCode(@RequestParam(value = "code") String code){
        if(!Strings.isNullOrEmpty(code)){
            storeConfig.setCode(code);
            return OperateResponse.obtainSuccessResponse(true);
        }else{
            return OperateResponse.failResponse("授权码为空！");
        }
    }

    @RequestMapping("/adminManager")
    String adminManager(ModelMap map, HttpServletRequest request){
        String code = request.getParameter("code");
        if(Strings.isNullOrEmpty(code)){
            return "redirect:/admin/login";
        }else{
            tokenService.getToken(code);
        }
//        if(SimpleCache.getInstance().getCache("userInfo") == null){
//            return "login";
//        }
        map.addAttribute("userInfo", SimpleCache.getInstance().getCache("userInfo"));
        return "admin/main";
    }

    @RequestMapping("/distributionMember")
    String distributionMember(){
        return "member/members";
    }

    @RequestMapping("/getUserMemeber")
    @ResponseBody
    OperateResponse getUserMemeber(){
        return OperateResponse.obtainSuccessResponse(userService.getAllDistributionMember());
    }

    @RequestMapping("/getAllProduct")
    String getAllProduct(){
        return "product/allProduct";
    }


    @RequestMapping("/getAllMachine")
    String getAllMachine(){
        return "machine/machineBinding";
    }

    @RequestMapping("/getAllTrades")
    String getAllTrades(){
        return "order/tradesList";
    }

}
